package com.smartguard.websocket;


import com.smartguard.bean.ClientEvent;
import com.smartguard.bean.Device;
import com.smartguard.bean.UserInfo;
import com.smartguard.service.DeviceService;
import com.smartguard.service.UserInfoService;
import com.smartguard.service.UserService;
import com.smartguard.util.MailUtil;
import com.smartguard.util.MessageUtil;

public class EventHandler {
	
	private static UserInfoService userInfoService = new UserInfoService();
//	private static UserService userService = new UserService();
	private static DeviceService deviceService = new DeviceService();
	
	/**
	 * 检测是否已经开启群组监控，是否接收监控消息并作出反应
	 * @return
	 */
	public static boolean checkMonitor(String deviceHash) {
		Device device = deviceService.findDeviceByDeviceHash(deviceHash);
		String userName = device.getUsername();
		UserInfo userInfo = userInfoService.findUserInfo(userName);
		return userInfo.getMonitor();
	}

	public static void handleSensor(ClientEvent clientEvent){
		// TODO 1发送短信
		// TODO 2通知其他用户
		MessageUtil.sendMessageForSensor(clientEvent.getDeviceHash());
		// TODO 3socket 通知钥匙端
		
		// 发送邮件
		DeviceService deviceService = new DeviceService();
		String username = deviceService.findDeviceByDeviceHash(clientEvent.getDeviceHash()).getUsername();
		UserInfoService infoService = new UserInfoService();
		String toEmail = infoService.findUserInfo(username).getContactEmail();
//		UserInfo userInfo = infoService.findUserInfo(username);
//		String toEmail = userInfo.getContactEmail();
		MailUtil.sendEmail(username, toEmail);
		System.out.println("send email");
		
	}
}
